import Vue from 'vue'
import VueRouter from 'vue-router'
import login from './components/login.vue'
import home from './components/home.vue'
import Welcome from './components/welcome.vue'
import User from './components/user/user.vue'
import Auth from './components/auth/auth.vue'
import Role from './components/auth/role.vue'
import Cate from './components/goods/cate.vue'
import './assets/css/global.css'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: login },
    {
      path: '/home',
      component: home,
      redirect: 'welcome',
      children:
    [
      { path: '/welcome', component: Welcome },
      { path: '/user', component: User },
      { path: '/auth', component: Auth },
      { path: '/role', component: Role },
      { path: '/cate', component: Cate }
    ]
    }
  ]
})

// 挂载路由守卫
router.beforeEach((to, from, next) => {
  // to表示要访问的路径
  // from代表哪个路径来
  // next是一个函数表示放行 next('/login') 强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    return next('/login')
  }
  next()
})

export default router
